const db = uniCloud.database();
const dbCmd = db.command;

module.exports = {
	
	add({
		user_id,
		name,
		tel,
		province,
		city,
		area,
		areaCode,
		detail,
		is_default
	}){
		return db.collection('address').add({
			user_id,
			name,
			tel,
			province,
			city,
			area,
			areaCode,
			detail,
			is_default,
			address: province+city+area+detail
		})
	},
	
	list(user_id){
		return db.collection('address').where({user_id}).get()
	},
	
	async setDefault(user_id,_id){
		let update = await db.collection('address').where({
			user_id: dbCmd.eq(user_id)
		}).update({
			is_default: false
		});
		let def = await db.collection('address').doc(_id).update({
			is_default: true
		})
		return def
	},
	
	get(_id){
		return db.collection('address').doc(_id).get()
	},
	
	save(_id,{
		name,
		tel,
		province,
		city,
		area,
		detail,
	}){
		return db.collection('address').doc(_id).update({
			name,
			tel,
			province,
			city,
			area,
			detail,
			address: province+city+area+detail
		})
	},
	
	delete(_id){
		return db.collection('address').doc(_id).remove()
	}
	
}
